message("1.version message")
cmake_minimum_required(VERSION 3.12.2)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

message("2.specify cross compilers and tools")
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfloat-abi=softfp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=softfp")
set(LIBS_TO_LINK libgcc.a -lc -lm)
set(CMAKE_C_FLAGS "-mcpu=cortex-m3 -mthumb -g -Os -ffreestanding -nostdlib")
set(CMAKE_EXE_LINKER_FLAGS "-lgcc")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mcpu=cortex-m3 -mthumb -Os -ffunction-sections -fdata-sections -nostdlib -nostartfiles")

 
message("3.project settings")
project(Project  C CXX ASM)

message("4.add_compile_options settings")
add_compile_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
add_compile_options(-Og)

message("5.add_definitions settings")
add_definitions(-DUSE_HAL_DRIVER -DSTM32F103xB -DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD)
 
message("6.include_directories")
include_directories(./CORE)
include_directories(./HARDWARE/_CAN ./HARDWARE/KEY  ./HARDWARE/LCD ./HARDWARE/LED)
include_directories(./STM32F10x_FWLib/inc)
include_directories(./SYSTEM/delay  ./SYSTEM/sys ./SYSTEM/usart ./SYSTEM/printf)
include_directories(./USER)

message("7.GLOB_RECURSE")
file(GLOB_RECURSE CORE   ./CORE/*.* )
file(GLOB_RECURSE HARDWARE  
    ./HARDWARE/_CAN/*.c 
    ./HARDWARE/KEY/*.c  
    ./HARDWARE/LCD/*.c 
    ./HARDWARE/LED/*.c)
file(GLOB_RECURSE FWLIB  ./STM32F10x_FWLib/src/*.c)
file(GLOB_RECURSE SYSTEM    
    ./SYSTEM/delay/*.c 
    ./SYSTEM/sys/*.c 
    ./SYSTEM/usart/*.c 
    ./SYSTEM/printf/*.c
    ./SYSTEM/memcpy/*.c)
file(GLOB_RECURSE USER   ./USER/*.c)
set(FLASH_LD    ${CMAKE_SOURCE_DIR}/CORE/STM32F103ZETx_FLASH.ld)
set(STARTUP_HD  ${CMAKE_SOURCE_DIR}/CORE/startup_stm32f10x_hd.s)
 
message("8.add_link_options")
add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork)
add_link_options(-specs=nano.specs -specs=nosys.specs -u _printf_float)

message("9.add_subdirectory")
# add_subdirectory(STM32F10x_FWLib)
# add_subdirectory(HARDWARE)
# add_subdirectory(SYSTEM)
 
message("10.add_executable")
add_executable(${PROJECT_NAME}.elf ${CORE} ${FWLIB} ${SYSTEM} ${HARDWARE} ${USER} ${FLASH_LD} ${STARTUP_HD})

message("11.target_link_libraries")
target_link_libraries(${PROJECT_NAME}.elf C:/SoftwareDesignToolChain/gcc/lib/gcc/arm-none-eabi/10.3.1/libgcc.a)
set_target_properties(${PROJECT_NAME}.elf PROPERTIES
  LINK_FLAGS "-Wl,-T,${CMAKE_SOURCE_DIR}/CORE/STM32F103ZETx_FLASH.ld")
 
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

message("12.add_custom_command") 
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")